# Import dependencies...
from random import shuffle
import random
import time
import re
# Import dictionary...
text_file = open("dictionary.dat", "r")
wordlist_b = text_file.read().split(' ') # Splits the dictionary into a list
def anagram(word): # Creates the anagram function that can be used for other things...
    if word == False: # If the parameter is set to False
        word = input("Word: ") # Take input for the word to anagramize
    word = list(word) # Splits the word into a list
    shuffle(word) # Scrambles letters into an anagram
    word = ''.join(word) # Joins letters back together
    output = word # Sets the anagram as output
    return output # Returns the anagram
# Built in wordlist
foo = "utopia sphynx rythm zephyr monotonous trebuchet aspiring naturalize sleepwalking cricket follower sentience distressing regularity frisking bottlefeed reacher amit apt uncivilty abbey abruptly affix askew axiom azure bagpipes bandwagon banjo bayou bikini blitz bookworm boxcar boxful buckaroo buffalo buffoon cobweb croquet daiquiri disavow duplex dwarves equip exodus fishhook fixable foxglove galaxy galvanize gazebo gizmo glowworm guffaw haiku haphazard hyphen icebox injury ivory ivy jaundice jawbreaker jaywalk jazzy jigsaw jiujitsu jockey jovial joyful juicy jumbo kazoo keyhole khaki kilobyte kiosk kiwifruit knapsack larynx luxury marquis megahertz microwave mystify nightclub nowadays numbskull ovary oxidize oxygen pajama peekaboo pixel pizazz pneumonia polka quartz quiz quorum razzmatazz rhubarb rickshaw schizophrenia sphinx spritz squawk subway swivel topaz unknown unworthy unzip uptown vaporize vixen vodka vortex walkway waltz wavy waxy wheezy whiskey whomever wimpy wizard woozy xylophone yachtsman yippee youthful zigzag zilch zodiac zombie agnostic cellulose photosynthesis physiotherapy nymph liverpool genuine methodic preposterous akimbo avenue awkward axolotl abyss tangerine duster maraca pancake gypsy accentuate bevel chicory delegate elephantine firmament galling hoary inaccessible jettison kleptomania lactate maestro neutral ostracize pandemonium quintessence recrudescence sagacious tachograph unwonted valance whopper xenophobia yuppie zucchini difficult lucid"
wordlist_a = re.sub("[^\w]", " ",  foo).split() # Turns the built-in dictionary into a list
wordlist = wordlist_a + wordlist_b # Combines the internal and external dictionaries
terminate = False
# Actual game starts here.
while terminate == False: # Agitation engine can crash the game here
    puzz = random.choice(wordlist) # Pick a word from the list
    output = anagram(puzz) # Anagramizes the picked word
    print("PyGrams 2.3")
    print("Figure out the anagram!")
    print(output) # Prints the anagram
    print("You have 3 guesses.")
    print("Type !hint for a hint.")
    guesses = 3 # Allocates guesses
    hint = None # Removes the hint variable
    agitate = 0 # Agitation level at 0
    correct = False # User has not picked the correct word yet
    while guesses > 0: # As long as you have more than 0 guesses...
        guess = input("Guess: ") # Takes user's guess
        if guess == "!hint": # Checks if the user asked for a hint
            if hint == None:
                length = len(puzz) # Finds the length of the answer
                hintlist = list(puzz) # Converts the answer into a list
                hintend = length - 1 # Finds the last letter
                starlength = hintend - 1 # Finds how many asterisks are needed
                stars = "*" * starlength # Generates the string for the asterisks
                hint = hintlist[0] + stars + hintlist[hintend] # Combines the required strings to make a hint
                print(hint) # Prints the hint
                hint = 1 # Uses the hint variable to tell the next if chunk that it may run
            else:
                if hint == 1: # If you already asked for a hint...
                    if length > 4: # If the length of the answer is greater than 4...
                        starlength = starlength - 2 # Change starlength to 2 less than before
                        stars = "*" * starlength # Generate star string
                        hintend2 = hintend - 1 # Creates hintend2, which is 1 less than hintend
                        hint = hintlist[0] + hintlist[1] + stars + hintlist[hintend2] + hintlist[hintend] # Combines the required strings to make a hint
                        print(hint) # Prints the hint
                        # Does not set the hint to None or 1, so the else will run forever afterwards
                    else:
                        if agitate > 6: # Checks agitation level
                            print("THAT'S ENOUGH") # Player has exhausted agitation limit
                            guesses = 0 # Ends game in loss
                            terminate = True # Terminates game
                        else:
                            print("The answer is too short for another hint.")
                            agitate = agitate+1 # Increases agitation level
                else:
                    if agitate > 6: # Checks agitation level
                        print("THAT'S ENOUGH") # Player has exhausted agitation limit
                        guesses = 0 # Ends game in loss
                        terminate = True # Terminates game
                    else:
                        print("I'm not giving you any more hints!")
                        agitate = agitate+1 # Increases agitation level
        else:
            if guess == "!list": # Checks for a debug command
                print(wordlist) # Prints full wordlist
            else:
                if guess == "!dict": # Checks for a debug command
                    print(wordlist_b) # Prints dictionary alone
                else:
                    if guess == puzz: #If the guess is correct...
                        print("Correct!")
                        guesses = 0 # Ends the game
                        correct = True # User has picked the correct word
                    else:
                        print("Incorrect!") 
                        guesses = guesses-1 # Deducts a guess
                        print("%d guesses remaining" % guesses,) # Prints remaining guesses
    if correct == True: # If the "correct" switch is True...
        print("You Win!")
    else:
        print("You Lose!")
        if terminate == False: # Makes sure agitation didn't max out
            print("The answer was %s" % puzz) # Prints the answer
    time.sleep(5) # Pauses for 5 seconds before restarting
